package com.dcjt.firehotcarpad.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    public static String getNoNullString(String str,String deafult){
        if (str!= null && str.trim().length()> 0&& !str.trim().equals("undfine")&& !str.trim().equals("null")){
            return str;
        }
        return deafult;
    }


    public static boolean isEmpty(String str){
        if (str!= null && str.trim().length()> 0&& !str.trim().equals("undfine")&& !str.trim().equals("null")){
            return false;
        }
        return true;
    }

    /**
     * 去除中括号
     */
    public static String getStringNoKuohao(String str){
       return str.replaceAll("\\[", "").replaceAll("\\]", "");
//       return str.replaceAll("\\[|\\]", "");
    }

    /**
     * 判断是否为正确的 ip：端口
     * @param ipStr
     * @return
     */
    public static boolean isIPLegal(String ipStr){
        if(TextUtils.isEmpty(ipStr)){
            return false;
        }
        Pattern pattern = Pattern.compile("^((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(:([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$)");
        Matcher matcher=pattern.matcher(ipStr);
        return matcher.find();
    }

    public static   String getNowTime(){
        Date date = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        return  formatter.format(date);
    }

    public static String uuid = "";

    @SuppressLint("MissingPermission")
    public static String getIMEI()  {
        if (uuid.isEmpty()) {
            uuid = UUID.randomUUID().toString();
        }
        return uuid;
    }

    @SuppressLint("SimpleDateFormat")
    public static  String getDate(Long date){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(date));
    }

}
